package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.sky.annotation.AotuFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0
 * @Author 郭腾高
 * @Date 2024/9/20 19:06
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /*新增套餐*/
    @Override
    //id回显
    public void insert(SetmealDTO setmealDTO) {
        //默认套餐为停售状态
        setmealDTO.setStatus(StatusConstant.DISABLE);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //在xml配置 keyProperty="id" 后可以拿到id
        //获取insert语句生成的主键值
        //查询遍历
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

            //遍历的同时菜品插入套餐对应的id
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //向 套餐关联菜品表 插入n条数据
            //批量插入
            setmealDishMapper.insertBach(setmealDishes);
        }
    /*分页查询套餐*/
    @Override
    public PageResult pageQuary(SetmealPageQueryDTO setmealPageQueryDTO) {
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }
    /*批量删除套餐*/
    @Override
    public void delete(List<Long> ids) {
        //查询套餐是否有启售状态的，有就删除失败
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐对应菜品集合
         setmealDishMapper.deleteBySetmealId(ids);
        //删除套餐
         setmealMapper.delete(ids);
    }
    /*查询回显套餐信息*/
    @Override
    public SetmealVO getBySetmealId(Long id) {
        //查询套餐信息
       Setmeal setmeal = setmealMapper.getBySetmealId(id);
       //根据套餐id查询套餐对应菜品
        SetmealVO setmealVO = new SetmealVO();
        List<SetmealDish> setmealDishes = setmealDishMapper.getListBySeatmealId(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        setmealVO.setUpdateTime(LocalDateTime.now());
        return setmealVO;
    }
    /*修改套餐信息*/
    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        //修改套餐信息
        setmealMapper.update(setmeal);
        //修改套餐对应菜品信息
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        Long setmealId = setmealVO.getId();
        //1.先将套餐对应菜品删除
        setmealDishMapper.deleteBySetmealIdOne(setmeal.getId());
        //遍历插入套餐菜品对应套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //2.重新插入套餐对应菜品信息
        setmealDishMapper.insertBach(setmealDishes);
    }
    /*套餐启售停售*/
    @Override
    public void changeStatu(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status == StatusConstant.ENABLE){
            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.changeStatus(status,id);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}




